//
//  YFPictureView.swift
//  微博
//
//  Created by 余仿 on 16/4/13.
//  Copyright © 2016年 YF. All rights reserved.
//

import UIKit

import SDWebImage

/// 该类用于处理图片显示相关的业务
private let reuseIdentifier = "pictureCell"
let picMargin:CGFloat = 3
let picMaxWidth = screenWidth - 2 * statusCellMargin
let picWidth = (picMaxWidth - 2 * picMargin) / 3
let picHeight = picWidth

class YFPictureView: UICollectionView, UICollectionViewDataSource{

    //搭建界面,界面取决于布局,布局取决于图片数量,面向ViewModel
    var picUrls:[YFPicture]?{

        didSet{

            //根据获得的图片链接的个数,计算self的宽高
            let  pictureViewSize = caculateSize()

            //布局自己
            //为什么是update???因为在之前已经设置啦!!!并且,每次来是要改啊
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(pictureViewSize)
            }

            //刷新
            reloadData()
        }
    }


    //MARK:根据图片的个数,计算一条微博中图片视图的大小
    func caculateSize() -> CGSize{

        let layout = self.collectionViewLayout as!UICollectionViewFlowLayout
        layout.itemSize = CGSize(width: picWidth, height: picHeight)

        let count = picUrls?.count ?? 0

        if count == 0{//没有图片

            return CGSizeZero

        }

        if count == 1 {//一张图片

            //collectionView的大小和其中图片的大小一样
            //设置图片大小
            let layout = self.collectionViewLayout as!UICollectionViewFlowLayout

            //设置图片的大小
            let imageSize = CGSize(width: 100, height: 150)

            //单张图片的时候,collectionView的大小应该和其中的item的大小一样
            layout.itemSize = imageSize

            return  layout.itemSize
        }

        if(count == 4){//四张图片

            let width =  2 * picWidth + picMargin
            return  CGSize(width:width, height: width)

        }

            //计算行数
            let row = CGFloat((count - 1) / 3 + 1)

            let heigth = CGFloat(row) * picWidth + (CGFloat(row) - 1) * picMargin

            return  CGSize(width: picMaxWidth, height: heigth)

    }

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {

        let flowlayout = UICollectionViewFlowLayout()

        flowlayout.itemSize = CGSize(width: picWidth, height: picHeight)

        flowlayout.sectionInset = UIEdgeInsetsZero

        flowlayout.minimumInteritemSpacing = picMargin

        flowlayout.minimumLineSpacing = picMargin

        super.init(frame: frame, collectionViewLayout: flowlayout)

        self.dataSource = self

        self.scrollEnabled = false

        registerClass(YFPictureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

// MARK: - 数据源方法
extension YFPictureView{

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return picUrls?.count ?? 0
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! YFPictureCell

        cell.backgroundColor = randomColor
        cell.picURLString = picUrls![indexPath.item].thumbnail_pic

        return cell
    }
}

//MARK:自定义Cell
extension YFPictureView{

    class YFPictureCell :UICollectionViewCell {

        var picURLString:String?{

            didSet{

                picture.sd_setImageWithURL(NSURL(string: picURLString ?? ""))
            }
        }

        //控件懒加载
        private lazy var picture:UIImageView = {
            let p = UIImageView()

            p.contentMode = .ScaleAspectFill

            p.clipsToBounds = true

            return p
        }()
        //添加到父视图
        override init(frame: CGRect) {
            super.init(frame: frame)
            
            setupUI()
        }
        
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
        
        //布局
        func setupUI(){

            addSubview(picture)

            picture.snp_makeConstraints { (make) -> Void in
                make.edges.equalTo(self.contentView)
                //make.top.left.right.bottom.equalTo(self.contentView)
            }
        }
    }
    
}


